$(function(){
	
	var pt = ""
	var oid = ""
	
	$('.pt li').on('click', function () {
        $(this).addClass('selected').siblings().removeClass('selected');
        
        pt = $(this).attr("data")
        
    })
	
    //href="${ctxPath}/createOrder.html?tid=${data.id}"
    $("#topay").on("click",function(){
    	
    	oid = $("#oid").val()+"";
    	
    	if(pt==""){
    		$.message({type:'warning',content:"请选择支付方式"});
    		return false
    	}
    	
    	if(oid==""){
    		$.message({type:'error',content:"参数错误"});
    		return false
    	}
    	topay(oid,pt)
    })
    
    function topay(oid,pt){
		$.ajax({
			url:"/topay",
			type:"GET",
			data:{orderNumber:oid,paytype:pt},
			contentType:"application/json",
			dataType:"json",
			success:function(data){
				if(data.code == 200){
					var ht = data.data
					if(pt=="alipay"){
						console.log(data)
						const div = document.createElement('divform');
						div.innerHTML = ht;
						document.body.appendChild(div);
						let len = document.forms.length - 1;
						document.forms[len].setAttribute('target', '_blank');
						document.forms[len].submit();
					}
					if(pt=="wechatpay"){
						$("#paycode").qrcode({
							render: "canvas",
							text:utf16to8(ht.codeUrl),
							width: 200,
							height: 200,
							foreground: "#000",
							background: "#FFF"
						});
						$("#payCodeBox").attr("style","display:block;")
						console.log(ht.codeUrl)
					}
					//getPayStatus()
				}else{
					$.message({type:'error',content:data.message});
		    		return false
				}
			}
		})
	}
	
	//中文编码格式转换
    function utf16to8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }
    
    function getPayStatus(){
		$.ajax({
			url:"/queryPayStatus",
			type:"GET",
			data:{orderNumber:oid,paytype:pt},
			contentType:"application/json",
			dataType:"json",
			success:function(data){
				if(data.code == 200){
					console.log(data)
					timeot = setTimeout(getPayStatus,1000);
				}
			}
		})
	}
	
	
	$("#login").on("click",function(){
		if(!getcode){
			login()
		}
		$("#zz").attr("style","display:block;")
	})
	
	$("#loginBtn").on("click",function(){
		if(!getcode){
			login()
		}
		$("#zz").attr("style","display:block;")
	})
	
	$("#closeLogin").on("click",function(){
		$("#zz").attr("style","display:none;")
	})
	
	$("#closePay").on("click",function(){
		$("#paycode").html("")
		$("#payCodeBox").attr("style","display:none;")
	})
	
	
	function login(){
		$.ajax({
			url:"/getLoginQrCode",
			type:"GET",
			data:{},
			contentType:"application/json",
			dataType:"json",
			success:function(data){
				if(data.code == 200){
					var img = "data:image/png;base64,"+data.data.qrcode
					getcode = true
					$("#qrcode").attr("src",img)
					$("#qrcode").attr("style","display:block;")
					$(".qrload").attr("style","display:none;")
					stid = data.data.stid
					getState()
				}
			}
		})
	}
	
	function getState(){
		$.ajax({
			url:"/getLoginStatus",
			type:"GET",
			data:{stid:stid},
			contentType:"application/json",
			dataType:"json",
			success:function(data){
				if(data.code == 200){
					if(data.data.state == 0 || data.data.state == 1){
						timeot = setTimeout(getState,1000);
						if(data.data.state == 1){
							$(".succ").attr("style","display:flex;")
							$("#stips").html("扫码成功，请登录")
						}
					}else if(data.data.state == 2){
						clearInterval(timeot)
						location.reload(true)
					}else{
						clearInterval(timeot)
					}
				}
			}
		})
	}
})